package com.gxa.autoparts.portal.service.impl;

import com.gxa.autoparts.core.pojo.Cart;
import com.gxa.autoparts.core.pojo.Goods;
import com.gxa.autoparts.core.pojo.GoodsSku;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import com.gxa.autoparts.core.util.JsonUtil;
import com.gxa.autoparts.portal.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author R
 * @data 2022-01-07 14:31
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class CartServiceImpl implements CartService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    private static final String CART_CACHE = "cart:";
    private static final String CART_SKU_CACHE = "goodsSku:";
    private static final String SKU_CACHE = "SKU:";
    private static final String GOOD_CACHE = "GOOD:";

    @Override
    public void addCartInfo(Long userId,Long skuId,Long number){
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        hashOps.putIfAbsent(CART_CACHE + userId, CART_SKU_CACHE + skuId, number);
    }

    @Override
    public Map getCart(Long userId){
        Map<String,Object> resultMap = new HashMap<>();
        List<Map<String,Object>> list = new ArrayList<>();
        Double totalPrice = 0D;
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        // 获取缓存里的购物车与购物车项
        Map<Object, Object> entries = hashOps.entries(CART_CACHE+userId);
        Cart cart = new Cart();
        Set<Map.Entry<Object, Object>> entries1 = entries.entrySet();
        Iterator<Map.Entry<Object, Object>> iterator = entries1.iterator();
        while (iterator.hasNext()){
            Map<String,Object> infoMap = new HashMap<>();
            Map.Entry<Object, Object> next = iterator.next();
            String s = next.toString();
            String[] split = s.split(":");
            String s1 = split[1];
            String[] split1 = s1.split("=");
            String s2 = split1[0];
            String s3 = split1[1];
            // 购物车项对应的skuId以及数量
            Integer skuID = Integer.parseInt(s2);
            Integer number = Integer.parseInt(s3);
            // 通过该skuId找到对应的sku缓存
            Map<Object, Object> entries2 = hashOps.entries(SKU_CACHE + skuID);
            GoodsSku goodsSku = JsonUtil.mapToObject(entries2, GoodsSku.class);

            Double singlePrice = goodsSku.getPrice()*number;
            totalPrice = totalPrice + singlePrice;
            infoMap.put("singlePrice",singlePrice);
            infoMap.put("skuId",skuID);
            infoMap.put("price",goodsSku.getPrice());
            infoMap.put("markPrice",goodsSku.getMarkPrice());
            infoMap.put("number",number);
            Long goodsId = goodsSku.getGoodsId();
            Map<Object, Object> entries3 = hashOps.entries(GOOD_CACHE + goodsId);
            Goods goods = JsonUtil.mapToObject(entries3, Goods.class);
            infoMap.put("img",goods.getImg());
            Map<String,String> map = JsonUtil.readToObject(goodsSku.getProductSpecs(), Map.class);
            Set<Map.Entry<String, String>> entries4 = map.entrySet();
            Iterator<Map.Entry<String, String>> iterator2 = entries4.iterator();
            StringBuilder detail = new StringBuilder();
            detail.append(goods.getName());
            while (iterator2.hasNext()){
                Map.Entry<String, String> next2 = iterator2.next();
                detail.append(next2.getValue());
            }
            infoMap.put("detail",detail.toString());
            list.add(infoMap);
        }
        resultMap.put("cartInfo",list);
        resultMap.put("totalPrice",totalPrice);
        return resultMap;
    }

    @Override
    public void updateCartInfo(Long userId, Long skuID,Long number) {
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        hashOps.put(CART_CACHE+userId,CART_SKU_CACHE+skuID,number);
    }

    @Override
    public void addSkuNumber(Long userId, Long skuID) {
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        hashOps.increment(CART_CACHE+userId,CART_SKU_CACHE+skuID,1L);
    }

    @Override
    public void cutSkuNumber(Long userId, Long skuID) {
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        Long increment = hashOps.increment(CART_CACHE + userId, CART_SKU_CACHE + skuID, -1L);
        if (increment <= 0){
            deleteCartInfo(userId,skuID);
        }
    }

    @Override
    public Boolean deleteCartInfo(Long userId, Long skuId) {
        HashOperations<String, Object, Object> hashOps = redisTemplate.opsForHash();
        Long delete = hashOps.delete(CART_CACHE + userId, CART_SKU_CACHE + skuId);
        return delete > 0;
    }

}
